<?php

class Application_Model_Single extends Application_Model_DomainObject{

    protected $_id;
    protected $_agentid;//发货渠道
    protected $_outstoreno;//出库号
    protected $_changeno;//改单号
    protected $_turnno;//转单号
    protected $_channel;//发货渠道
    protected $_destination;//目的地
    protected $_num;//件数
    protected $_weight;//实重
    protected $_bagweight;//材重
    protected $_name;//品名
    protected $_zipcode;//邮编
    protected $_recipients;//收件人
    protected $_crdatetime; //时间
    protected $_ischange;//是否已出转单号


    public static function byMaxTime($agentid)
    {
        $where[" agentid = "] = $agentid;
        $result =  self::getDao()->queryAll($where," crdatetime desc limit 1");
        if(count($result)==0){
            return  "2013-04-01";
        } else {
            $maxtime = $result[0]->crdatetime;
            return date('Y-m-d',strtotime("$maxtime -4 day"));;
        }
       
    }
    
    public static function bySingle($agentno)
    {
        $where[" outstoreno = "] = $agentno;
        return self::getDao()->queryOne($where);
    }
    public static function getFilter($name,$dateinbegin,$dateinend,$agentid,$pagesize,$pageindex){
        $where = array();
        $and = '';
        $or = '';
        $order = 'crdatetime desc';
        if ($name != '') {  
            $where[$and . '( outstoreno like '] = "%".$name."%";
            $or = " or ";
            $where[$or . ' changeno like '] = "%".$name."%";
            $where[$or . ' turnno like '] = "%".$name."%"; 
            $where[" ) and "] = 1;
            $and = ' and ';
        }
        if ($dateinbegin != ''){
            $where[$and . ' crdatetime >='] = $dateinbegin;
            $and = ' and ';
        }
        if ($dateinend != ''){
            $where[$and . ' crdatetime <='] = $dateinend;
            $and = ' and ';
        }
        if ($agentid != ''){
            $where[$and . ' agentid ='] = $agentid;
            $and = ' and ';
        }
        return Application_Model_Single::getDao()->queryPage($where,$order,$pagesize,$pageindex);
    }
    public function getOutnoCost()
    {
        $where[" channelNo = "] = $this->_outstoreno;
        return Application_Model_CostPrice::getDao()->queryOne($where);
    }
    
    public function getAgentName()
    {
        $where[" id = "] = $this->_agentid;
        return Application_Model_AgentCompany::getDao()->queryOne($where);
    }
    
    public static function byAllSigle()
    {
        $where["( ischange = "] = 0;
        $where[" or ischange is null) and "] = 1;
        return self::getDao()->queryAll($where);
    }
    
    
}
